Page({
	data: {
		nickName: 'name',
		avatarUrl: '/icon/225默认头像.png',
		showDialog: false,
		isLogin: false,
		isClear:false
	},
	login: function () {
		wx.login({
			success: ({code}) => {
				if (code) {
					// res.eventChannel.emit('logIn',{avatarUrl:})

          console.log(code)

					this.setData({
						isLogin:true
					})
				}else{
					wx.showToast({
						title: 'fileToLogin',
						duration: 0,
						icon: icon,
						image: 'image',
						mask: true,
						success: (res) => {},
						fail: (res) => {},
						complete: (res) => {},
					})
				}
			},
		})
		wx.request({
			url: 'http://localhost:8001/user/setUser',
			method:'POST',
			data:{
				nickName:this.nickName,
				avatarUrl:this.avatarUrl
			}
		})
		this.setData({
			showDialog: true
		})
	},
	chooseAvatar(event) {
		const {
			avatarUrl
		} = event.detail
		this.setData({
			avatarUrl: avatarUrl
		})
		wx.setStorageSync('avatarUrl', avatarUrl );
	},
	navigateToSetting: function () {
		wx.navigateTo({
			url: '/pages/Setting/settings',
			events:{
				currentevent:(res)=>{
					console.log("res");
				}
			},
			success: function (res) {
				console.log("成功跳转到设置页面");
			},
			fail: function (err) {
				console.error("跳转到设置页面失败", err);
			}
		});
	},
	navigateTosaving: function () {
		wx.navigateTo({
			url: '/pages/saving/saving',
			success: function (res) {
				console.log("成功跳转到保存的记录页面");
			},
			fail: function (err) {
				console.error("跳转到保存的记录页面失败", err);
			}
		});
	},
	navigateToFeedback: function () {
		wx.navigateTo({
			url: '/pages/feedback/feedback',
			success: function (res) {
				console.log("成功跳转到问题反馈页面");
			},
			fail: function (err) {
				console.error("跳转到问题反馈页面失败", err);
			}
		});
	},
	showClearRecordsModal: function () {
		wx.showModal({
			title: '确认清空记录',
			content: '您确定要清空所有记录吗？',
			cancelText: '取消',
			confirmText: '确认',
			success: function (res) {
				if (res.confirm) {
					this.setData({
						isClear:true
					})
					
					console.log('用户点击了确认');
					wx.showLoading({
						title: '正在清空...',
						mask: true
					});
					wx.request({
						url: 'http://localhost:8001/record/clearRecords',
						method:'POST',
		       	data:{
                isClear:isClear
		       	}
					})
				} else if (res.cancel) {
					console.log('用户点击了取消');
				}
			},

		});
		wx.hideLoading()
	},

	onLoad(){
		let avatarUrl = this.data.avatarUrl;
		let nickName = this.data.nickName;
		this.setData({
			avatarUrl:avatarUrl,
			nickName:nickName
		})
	},
	onShow() {
		let avatarUrl = wx.getStorageSync('avatarUrl');
		let nickName = wx.getStorageSync('SetnickName');
		if (avatarUrl!=''&&nickName!='') {
			this.setData({
			avatarUrl:avatarUrl,
			nickName:nickName
		})
		}
		
		if (typeof this.getTabBar === 'function' && this.getTabBar()) {
			this.getTabBar().setData({
				selected: 1
			})
		}
	},

})